#pragma once

#include <iostream>
#include <pthread.h>
#include <functional>

namespace __Thread
{
    template <typename T>
    using _thread_func = std::function<void(T &)>;

    template <typename T>
    class Thread
    {
        pthread_t _Tid;
        std::string _Thread_name;
        bool _Is_running;

        _thread_func<T> _func; // 线程回调函数
        T _data;               // 线程回调参数

    public:
        Thread(const std::string &__Thread_name, _thread_func<T> __func, T &__data)
            : _Thread_name(__Thread_name), _func(__func), _data(__data)
        {
        }

        ~Thread() {}

        std::string Threadname()
        {
            return _Thread_name;
        }

        static void *ThreadRun(void *args)
        {
            Thread *thread = static_cast<Thread *>(args);
            thretad->_func(_data);
            return nullptr;
        }

        bool start()
        {
            int n = pthread_create(&_Tid, nullptr, ThreadRun, this);
            if (n != 0)
                return false;

            _Is_running = true;
            return true;
        }

        bool IsRunning()
        {
            return _Is_running;
        }

        bool Join()
        {
            if (!_Is_running)
                return true;

            int n = pthread_join(tid, nullptr);
            if (n == 0)
            {
                _Is_running = false;
                return true;
            }
            return false;
        }
    };

}